from ._builtin import Page, WaitPage
from otree.api import Currency as c, currency_range
from .models import Constants


class Introduction(Page):
    pass



class Request(Page):
    # //对照组1 时间压力为20 20 15 15 15 15 15 15。。。。。。。 隐藏显示
    def get_timeout_seconds(self):
        if self.round_number==1 or self.round_number == 2:
            return 7
        else:
            return 5
    timer_text = '距离您提议结束还剩:'

    form_model = 'player'
    form_fields = ['request','quit_game']

    def error_message(self, values):
        group = self.group
        if values['request'] is None:
            values['request'] = 0
        if values['request'] < 0.04 * group.point_pool and values['quit_game'] == False:
            print("123")
            return '为防止误操作，值应大于资产剩余数额的4%'

    def vars_for_template(self):
        group = self.group
        return dict(
            request_label='请输入您的需求（不超过{}） '.format(group.point_pool),
            other_player_request1=self.player.other_player()[0].request,
            other_player_request2=self.player.other_player()[1].request,
            other_player_quit_game1=self.player.other_players_quit_game()[0].quit_game,
            other_player_quit_game2=self.player.other_players_quit_game()[1].quit_game,
            other_player1=self.player.other_player()[0],
            other_player2=self.player.other_player()[1],
            pre_other_player1=self.player.previous_round_other_demand1(),
            pre_other_player2=self.player.previous_round_other_demand2(),
            game_round=self.player.game_round
        )

    def before_next_page(self):
        player = self.player
        group = self.group
        timeout_happened = self.timeout_happened
        if player.request is None:
            player.request = 0
        if timeout_happened:
            player.request = 0
            player.not_refer = True
            group.not_refer = True


class ResultsWaitPage1(WaitPage):
    template_name = 'S_bargaining2_second/ResultsWaitPage.html'
    title_text = "等待页面"
    body_text = "请等待其他玩家完成测试。"
    after_all_players_arrive = 'set_point_pool'
    def vars_for_template(self):
        group=self.group
        return dict(
            request_label='请输入您的需求（不超过{}） '.format(group.point_pool),
            other_player_request1=self.player.other_player()[0].request,
            other_player_request2=self.player.other_player()[1].request,
            other_player_quit_game1=self.player.other_players_quit_game()[0].quit_game,
            other_player_quit_game2=self.player.other_players_quit_game()[1].quit_game,
            other_player1=self.player.other_player()[0],
            other_player2=self.player.other_player()[1],
            pre_other_player1 =self.player.previous_round_other_demand1(),
            pre_other_player2 =self.player.previous_round_other_demand2(),
            game_round=self.player.game_round
        )
class ResultsWaitPage2(WaitPage):
    template_name = 'S_bargaining2_second/ResultsWaitPage2.html'
    title_text = "等待页面"
    body_text = "请等待其他玩家完成测试。"
    after_all_players_arrive = 'set_payoffs'
    def vars_for_template(self):
        group=self.group
        return dict(
            request_label='请输入您的需求（不超过{}） '.format(group.point_pool),
            other_player_request1=self.player.other_player()[0].request,
            other_player_request2=self.player.other_player()[1].request,
            other_player_quit_game1=self.player.other_players_quit_game()[0].quit_game,
            other_player_quit_game2=self.player.other_players_quit_game()[1].quit_game,
            other_player1=self.player.other_player()[0],
            other_player2=self.player.other_player()[1],
            pre_other_player1 =self.player.previous_round_other_demand1(),
            pre_other_player2 =self.player.previous_round_other_demand2(),
            game_round=self.player.game_round
        )


class Results(Page):
    timeout_seconds = 10
    timer_text = '距离页面跳转还有:'
    def vars_for_template(self):
        #这里是放要传到模板里的数据
        #其他玩家的需求分数的总和
        return dict(other_player_request1=self.player.other_player()[0].request,
                    other_player_request2=self.player.other_player()[1].request,
                    other_player_quit_game1=self.player.other_players_quit_game()[0].quit_game,
                    other_player_quit_game2=self.player.other_players_quit_game()[1].quit_game,
                    other_player1= self.player.other_player()[0],
                    other_player2 = self.player.other_player()[1])
                    # pre_other_player=self.player.previous_round_other_demand())
    #比赛结束之后 前往哪个页面
    def app_after_this_page(self, upcoming_apps):
        group = self.group
        if group.end_game==False:
            # 继续
            pass
        else:
            # 结束比赛
            return upcoming_apps[-1]

        # if player.whatever:
        #     return upcoming_apps[0]


page_sequence = [ResultsWaitPage1,Request, ResultsWaitPage2, Results]
